﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Dal;

namespace Control.Bus
{
    public class BusImportBillDetail
    {
        public int ID { get; set; }
        private BusImportBill bill;
        public BusImportBill Bill
        {
            set{bill=value;}
            get
            {
                if(bill==null)
                    bill=new BusImportBill();
                return bill;
            }
        }
        public int Quantity { get; set; }
        private BusProduct prod;
        public BusProduct Prod
        {
            set { prod = value; }
            get
            {
                if (prod == null)
                    prod = new BusProduct();
                return prod;
            }
        }

        public string KeProd
        {
            get { return Prod.ShelfName; }
        }
        public string MaProd
        {
            get { return Prod.ProductCode; }
        }
        public string Date
        {
            get { return Bill.Date.ToShortDateString(); }
        }
        private bool isSaved;
        public bool IsSaved
        {
            get
            {
                if (isSaved == null)
                    isSaved = false;
                return isSaved;
            }
            set { isSaved = value; }
        }
        
        public void Insert()
        {
            ID = DalImportDetail.Insert(this);
            BusWareHouse Wh = BusWareHouse.GetWHByDateAndProd(Bill.Date, Prod);

            try
            {
                Wh.Import += Quantity;
                Wh.Store += Quantity;
                Wh.UpDate();
            }
            catch
            {
                DateTime billdate = this.Bill.Date;
                Wh.Date = billdate;
                Wh.Import = Quantity;
                Wh.Store = Prod.GetTonKho(billdate)+Quantity;
                Wh.Prod = Prod;
                Wh.Insert();
            }

        }

        public void Delete()
        {
            DalImportDetail.Delete(this);
            BusWareHouse Wh = BusWareHouse.GetWHByDateAndProd(Bill.Date, Prod);

            try
            {
                Wh.Import -= Quantity;
                Wh.Store -= Quantity;
                Wh.UpDate();
            }
            catch
            {
                
            }

        }

        public void UpDate()
        {
            BusImportBillDetail bd = DalImportDetail.GetByID(this.ID);
            DalImportDetail.UpDate(this);
            BusWareHouse Wh = BusWareHouse.GetWHByDateAndProd(Bill.Date, Prod);

            try
            {
                Wh.Import -= (bd.Quantity-Quantity);
                Wh.Store -= (bd.Quantity - Quantity);
                Wh.UpDate();
            }
            catch
            {

            }
        }


        public static BusImportBillDetail GetByID(int p)
        {
            return DalImportDetail.GetByID(p);
        }

        public static List<BusImportBillDetail> Find(string ProdCode, DateTime fromDate, DateTime toDate)
        {

            DateTime newfromDate = new DateTime((int)fromDate.Year, (int)fromDate.Month, (int)fromDate.Day, 0, 0, 1);
            DateTime newtoDate = new DateTime((int)toDate.Year, (int)toDate.Month, (int)toDate.Day, 23, 59, 59);
            return DalImportDetail.Find(ProdCode, newfromDate, newtoDate);
        }
    }
}
